let vm = new Vue({
    el: "div",
    data: {
        repass: "",
        user: {
            username: "",
            password: "",
            phone: ""
        }
    },
    methods: {
        prevent() {
            let event = window.event;

            if (event.keyCode == 32) {
                event.returnValue = false;
            }
        },
        reg() {
            let regex = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
            let username = vm.user.username;
            let password = vm.user.password;
            let repass = vm.repass;
            let phone = vm.user.phone;
            let allow = $("#allow").is(':checked');

            $('.error_tip2').hide();

            if (username.length < 1 || username.length > 10) {
                alert("请输入1-10个字符的用户名");
            } else if (password.length < 8 || password.length > 20) {
                alert("密码最少8位，最长20位");
            } else if (password !== repass) { // 检查确认密码
                alert("两次输入的密码不一致");
            } else if (!regex.test(phone)) {
                alert("你输入的手机号格式不正确");
            } else if (!allow) {
                $('.error_tip2').html('请勾选同意').show();
            } else {
                axios.post("/user/register", vm.user).then(function (response) {
                    let result = response.data;

                    if (result.code === 200) {
                        location.href = "/login.html";
                    } else {
                        alert(result.msg);
                    }
                });
            }
        }
    }
});